package com.hst.capacity.facade.rest;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.hst.capacity.application.biz.VehicleBiz;
import com.hst.capacity.domain.exception.BusinessException;
import com.hst.capacity.domain.model.request.vehicle.*;
import com.hst.capacity.domain.model.response.DefaultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;

import static com.hst.capacity.domain.model.response.DefaultVO.success;

/**
 *  车辆管理模块Controller，车辆的 CRUD，检索等。
 *
 * */
@RequestMapping("/vehicle")
@RestController
public class VehicleController {

    @Autowired
    private VehicleBiz vehicleBiz;


    /**
     *获取地区下的所有车辆类型
     * @param param
     * @return
     */
    @PostMapping("/type/list/region")
    private DefaultVO findList(@RequestBody @Valid VehicleTypeParam param){
        return success(vehicleBiz.findVehicleTypeByStationInfo(param));
    }

    /**
     *获取当前业务下所有车辆类型
     * @param param
     * @return
     */
    @PostMapping("/type/list")
    private DefaultVO findVehicleType(@RequestBody @Valid VehicleTypeParam param){
        return success(vehicleBiz.findVehicleType(param));
    }

    /**
     * 车辆信息分页条件检索
     * @param param
     * @return
     */
    @PostMapping("/search")
    private DefaultVO search(@RequestBody @Valid VehicleSearchParam param){
        return success(vehicleBiz.search(param));
    }

    /**
     *添加车辆
     * @param param
     * @return
     */
    @PostMapping("/add")
    private DefaultVO addVehicle(@RequestBody @Valid VehicleAddParam param) {
//        HashMap<String, String> returns = new HashMap<>();
//        returns.put("idVehicle",vehicleBiz.addVehicle(param));
//        MapBuilder<String, String> builder = MapUtil.builder("", "");
        return success(MapUtil.builder("idVehicle", vehicleBiz.addVehicle(param)).build());
    }
    /**
     *添加车辆保险
     * @param param
     * @return
     */
    @PostMapping("/add/insurance")
    private DefaultVO addInsurance(@RequestBody @Valid VehicleAddInsuranceParam param){
        return success(vehicleBiz.addInsurance(param).getIdVehicleInsurance());
    }

    /**
     * 添加车辆资质
     * @param param
     * @return
     */
    @PostMapping("/add/certificate")
    private DefaultVO addCertificate(@RequestBody @Valid VehicleAddCertificateParam param){
        return success(vehicleBiz.addCertificate(param));
    }
    /**
     *绑定车辆关系(暂废弃)
     * @param param
     * @return
     */
    @PostMapping("/add/relation")
    private DefaultVO addVehicleRelation(@RequestBody @Valid VehicleRelationParam param){
        return success(vehicleBiz.addVehicleRelation(param));
    }
    /**
     * 使用车辆(暂废弃)
     * @param
     * @return
     */
    @PostMapping("/active/relation")
    private DefaultVO activeVehicleRelation(@RequestBody Map<String,String> param){
        String id = param.get("idDriverCar");
        if (StrUtil.isEmpty(id))
            throw new BusinessException(4001,"关系ID[idDriverCar]不能为空");
        return success(vehicleBiz.activeVehicleRelation(id));
    }

    /**
     * 删除车辆绑定关系(暂废弃)
     * @param
     * @return
     */
    @PostMapping("/delete/relation")
    private DefaultVO deleteVehicleRelation(@RequestBody Map<String,String> param){
        String id = param.get("idDriverCar");
        if (StrUtil.isEmpty(id))
            throw new BusinessException(4001,"关系ID[idDriverCar]不能为空");
        return success(vehicleBiz.deleteVehicleRelation(id));
    }
    /**
     * 获取司机已绑定车辆列表(暂废弃)
     * @param
     * @return
     */
    @PostMapping("/list/relation")
    private DefaultVO listVehicleRelation(@RequestBody Map<String,String> param){
        String id = param.get("idDriver");
        if (StrUtil.isEmpty(id))
            throw new BusinessException(4001,"司机ID[idDriver]不能为空");
        return success(vehicleBiz.listVehicleRelation(id));
    }

    /**
     * 车辆编辑查询
     * @param
     * @return
     */
    @PostMapping("/save/select")
    private DefaultVO vehicleEditSelect(@RequestBody Map<String,String> param){
        String id = param.get("idVehicle");
        String moduleName = param.get("moduleName");
        if (StrUtil.isEmpty(id=param.get("idVehicle")))
            throw new BusinessException(4001,"车辆ID[idVehicle]不能为空");
        if (StrUtil.isEmpty(moduleName))
            throw new BusinessException(4001,"查询模块名[moduleName]不能为空");
        return success(vehicleBiz.vehicleEditSelect(id,moduleName));
    }

    /**
     * 车辆删除
     * @param
     * @return
     */
    @PostMapping("/delete")
    private DefaultVO vehicleDelete(@RequestBody Map<String,String> param){
        String id = param.get("idVehicle");
        if (StrUtil.isEmpty(id))
            throw new BusinessException(4001,"车辆ID[idVehicle]不能为空");
        return success(vehicleBiz.vehicleDelete(id));
    }

    /**
     *获取所有车辆类型
     * @return
     */
    @PostMapping("/type/all")
    private DefaultVO findAllVehicleType(){
        return success(vehicleBiz.findAllVehicleType());
    }
    
}
